package cn.iocoder.idata.module.idata.service.batchTask;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.idata.module.idata.controller.admin.batchTask.vo.*;
import cn.iocoder.idata.module.idata.dal.dataobject.batchTask.BatchTaskDO;
import cn.iocoder.idata.framework.common.pojo.PageResult;

import cn.iocoder.idata.module.idata.convert.batchTask.BatchTaskConvert;
import cn.iocoder.idata.module.idata.dal.mysql.batchTask.BatchTaskMapper;

import static cn.iocoder.idata.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.idata.module.system.enums.ErrorCodeConstants.*;


/**
 * 批次任务 Service 实现类
 *
 * @author 智数源码
 */
@Service
@Validated
public class BatchTaskServiceImpl implements BatchTaskService {

    @Resource
    private BatchTaskMapper batchTaskMapper;

    @Override
    public Long createBatchTask(BatchTaskCreateReqVO createReqVO) {
        // 插入
        BatchTaskDO batchTask = BatchTaskConvert.INSTANCE.convert(createReqVO);
        batchTaskMapper.insert(batchTask);
        // 返回
        return batchTask.getId();
    }

    @Override
    public void updateBatchTask(BatchTaskUpdateReqVO updateReqVO) {
        // 校验存在
        validateBatchTaskExists(updateReqVO.getId());
        // 更新
        BatchTaskDO updateObj = BatchTaskConvert.INSTANCE.convert(updateReqVO);
        batchTaskMapper.updateById(updateObj);
    }

    @Override
    public void deleteBatchTask(Long id) {
        // 校验存在
        validateBatchTaskExists(id);
        // 删除
        batchTaskMapper.deleteById(id);
    }

    private void validateBatchTaskExists(Long id) {
        if (batchTaskMapper.selectById(id) == null) {
            throw exception(BATCH_TASK_NOT_EXISTS);
        }
    }

    @Override
    public BatchTaskDO getBatchTask(Long id) {
        return batchTaskMapper.selectById(id);
    }

    @Override
    public List<BatchTaskDO> getBatchTaskList(Collection<Long> ids) {
        return batchTaskMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BatchTaskDO> getBatchTaskPage(BatchTaskPageReqVO pageReqVO) {
        return batchTaskMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BatchTaskDO> getBatchTaskList(BatchTaskExportReqVO exportReqVO) {
        return batchTaskMapper.selectList(exportReqVO);
    }

}
